package Docker

import (
	"fmt"
	"os"
	"os/exec"
	"time"
	"runtime"

)
const (
	Red = "\033[31m Error: "
	Green = "\033[32m Success:"
	Yellow = "\033[33m Warning:"
	Reset = "\033[0m"
)

//检测容器是否安装
func Docker_check() bool {
	cmd := exec.Command("bash", "-c", "docker -v")
	err := cmd.Run()
	if err != nil {
		return false
	}
	return true
}

// 功能菜单
func Docker_menu() {
	var docker_menu = `
	======Docker菜单(常用功能)========
	  1.列出运行中容器
	  2.列出所有容器(包括停止的)
	  3.列出所有镜像
	  4.停止容器
	  5.启动容器
	  6.删除镜像
	  7.删除容器
	  8.卸载Docker
	  9.启动Docker
	  10.停止Docker
	  11.重启Docker
	  12.查看容器运行日志
	  13.容器控制台
	  0.返回主菜单
	请输入菜单编号：`
	for {
		var choice int
		clearLog()
		fmt.Printf("\033[34m")
		fmt.Print(docker_menu)
		fmt.Scanln(&choice)
		switch choice {
		case 1:
			// 1.列出容器
			Docker_list_container(false, true)
		case 2:
			// 2.列出所有容器
			Docker_list_container(true, true)
		case 3:
			// 2.列出镜像
			Docker_list_image(false, true)
		case 4:
			// 3.停止容器
			Docker_stop_container() 
		case 5:
			Docker_start_container()
		case 6:
			// 4.删除镜像
			Docker_list_image(true, true)
		case 7:
			// 5.删除容器
			DockerDleContainer()
		case 8:
			// 6.卸载Docker
			Docker_del()
		case 9:
			// 7.启动Docker
			Docker_start()
		case 10:
			// 8.停止Docker
			Docker_stop()
		case 11:
			// 9.重启Docker
			Docker_restart()
		case 12:
			// 10.查看容器运行日志
			Docker_logs()
		case 13:
			// 11.容器控制台
			Docker_exec()
		case 0:
			// 7.返回主菜单
			return
		default:
			//红色
			fmt.Println(Red,"输入错误，请重新输入",Reset)
			// reader := bufio.NewReader(os.Stdin)
			// // 清除缓存区中的回车符
			// _, _ = reader.ReadBytes('\n')
			time.Sleep(2 * time.Second)
		}
	}
}

// 主函数
func Docker_main() {
	install := Docker_check()
	if install == false {
		fmt.Println("Docker未安装")
		var choice string
		fmt.Printf("是否安装Docker？(y/n)")
		fmt.Scanln(&choice)
		if choice == "y" {
			cmd := exec.Command("bash", "-c", "curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun")
			cmd.Stdout = os.Stdout
			cmd.Stderr = os.Stderr
			err := cmd.Run()
			if err != nil {
				fmt.Println("Docker安装失败")
				time.Sleep(2 * time.Second)
				return
			}
			fmt.Println("Docker安装成功")
			time.Sleep(2 * time.Second)
		} else {
			//黄色
			fmt.Println(Yellow,"Docker没有安装，无法使用Docker功能",Reset)
			time.Sleep(2 * time.Second)
			return
		}

	}
	Docker_menu()
}


func Docker_del() {
	install := Docker_check()
	if install == true {
		fmt.Println("Docker已安装")
		var choice string
		fmt.Printf("是否卸载Docker？(y/n)")
		fmt.Scanln(&choice)
		if choice == "y" {
			cmd := exec.Command("bash", "-c", "curl -fsSL https://gitee.com/Liplay-1/YBOX_2/raw/master/software/DockerDel.sh | bash ")
			cmd.Stdout = os.Stdout
			cmd.Stderr = os.Stderr
			err := cmd.Run()
			if err != nil {
				fmt.Println("Docker卸载失败")
				time.Sleep(2 * time.Second)
				return
			}
			fmt.Println("Docker卸载成功")
			time.Sleep(2 * time.Second)
		} 
	}
}


func clearLog() {
	var cmd *exec.Cmd
	if runtime.GOOS == "windows" {
		cmd = exec.Command("cmd", "/c", "cls")
	} else {
		cmd = exec.Command("clear")
	}
	cmd.Stdout = os.Stdout
	cmd.Run()
}
